﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;

namespace CrmApplication.Models
{
    public class DomainModels
	{
		#region RegionMappingDomain
		
		public decimal Id{ get; set; }
        public string Name { get; set; }

		#endregion

		#region RegionOracleConnect

		private static OracleConnection connect;

        static DomainModels()
        {
            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["HomeOrcl"].ConnectionString;
            connect = new OracleConnection(connString);
        }

		#endregion

		#region RegionDomains
		/// <summary>
        /// get domains list
        /// </summary>
        /// <param name="searchDomain"></param>
        /// <returns></returns>
        public static List<DomainModels> GetDomain(string searchDomain)
        {
            string selectDomain = "where domain_name like '" + searchDomain + "%'";
            string selectString = "SELECT id_domain,domain_name from domains " 
                                    + selectDomain 
                                    + " order by domain_name ";

            OracleCommand loginComm = new OracleCommand(selectString);
            loginComm.Connection = connect;

            List<DomainModels> result = new List<DomainModels>();
            DomainModels result1 = null;
            try
            {
                connect.Open();
                using (OracleDataReader sdr = loginComm.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        result1 = new DomainModels();
                        result1.Id = sdr.GetDecimal(0);
                        result1.Name = sdr.GetString(1);                        
                        result.Add(result1);
                    }
                }
            }
            finally
            {
                connect.Close();
            }
            return result;
        }


        /// <summary>
        /// add new domain
        /// </summary>
        /// <param name="domainName"></param>
        public static void AddDomain(string domainName)
        {
            string insert = "INSERT INTO domains(domain_name) VALUES (:domainName)";

            OracleCommand usercomm = new OracleCommand(insert);

            usercomm.Parameters.Add(":domainName", domainName);

            usercomm.Connection = connect;
            try
            {
                connect.Open();
                usercomm.ExecuteNonQuery();
            }
            finally
            {
                connect.Close();
            }
        }

        /// <summary>
        /// creat domain array for json
        /// </summary>
        /// <param name="searchDomain"></param>
        /// <returns></returns>
        public static DomainContainer GetDomainArray(string searchDomain)
        {
            DomainModels[] data = new DomainModels[20];
            data = DomainModels.GetDomain(searchDomain).ToArray();

            DomainContainer container = new DomainContainer()
            {
                Records = data,
                Count = data.Length
            };

            return container;
		}
		#endregion
	}

    public class DomainContainer
    {
        public DomainModels[] Records { get; set; }
        public int Count { get; set; }
    }
}